package com.smartHire.controller.Business;

import com.alibaba.fastjson.JSONObject;
import com.smartHire.domain.Goods;
import com.smartHire.domain.Shop;

import com.smartHire.service.GoodsService;
import com.smartHire.service.ShopService;

import com.smartHire.util.HttpRequest;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.ArrayList;

@WebServlet(name = "FindGoodsServlet", value = "/FindGoodsServlet")
public class FindGoodsServlet extends HttpServlet{
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        int Good_id = Integer.parseInt(request.getParameter("goods_id"));
        System.out.println("sadsadasdasdasdasdsad"+Good_id);
        WebApplicationContext ctx = null;
        //使用框架中的方法，获取容器对象
        ServletContext sc = getServletContext();
        ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(sc);
      //  System.out.println("容器对象的信息========" + ctx);
        //获取service

        ArrayList list = new ArrayList();

        try{
            GoodsService goodsServiceservice  = (GoodsService) ctx.getBean("goodsService");
            ShopService shopService = (ShopService) ctx.getBean("shopService");

            Goods goods = new Goods();

            goods.setGood_id(Good_id);

            Goods goods1=goodsServiceservice.queryGoodsByID(goods);

            Shop shop=new Shop();
            shop.setShop_id(goods1.getShop_id());

            Shop shop1=shopService.queryShopById(shop);

            list.add(goods1);
            list.add(shop1);

           // System.out.println(goods1);
            String jsonStr = JSONObject.toJSONString(list);

            response.setCharacterEncoding("utf-8");
            response.getWriter().write(jsonStr);

         //   System.out.println(jsonStr);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }

    }

}
